package com.jojo.bubble.utils;

import com.jojo.bubble.entity.EncodeVo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @Author JoJo
 * @Date 2023/6/28 9:47
 * @Description
 * @Version 1.0
 */
public class MapUtil {

    public static EncodeVo getCityEncode(String cityName) {
        EncodeVo encodeVo = new EncodeVo();
        try {
            // 将城市名称进行 URL 编码
            String encodedCityName = URLEncoder.encode(cityName, "UTF-8");

            // 构建请求 URL
            String apiUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + encodedCityName + "&output=json&ak=LL4tVRxHjfU3Whobb9ZQwrU3MSxgGruV";

            // 发送 HTTP GET 请求
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 读取 API 响应数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 解析 JSON 数据
            // 这里使用的是百度地图的 API 响应格式，请根据实际使用的地图服务进行解析
            // 示例中假设 API 响应的数据结构为 {"result":{"location":{"lng":xxx,"lat":xxx}}}
            double cityLng = Double.parseDouble(response.toString().split("\"lng\":")[1].split(",")[0]);
            double cityLat = Double.parseDouble(response.toString().split("\"lat\":")[1].split("}")[0]);
            BigDecimal lng = BigDecimal.valueOf(cityLng);
            BigDecimal lat = BigDecimal.valueOf(cityLat);

            System.out.println("经度：" + lng);
            System.out.println("纬度：" + lat);

            encodeVo.setLat(lat);
            encodeVo.setLng(lng);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return encodeVo;
    }
    public static void main(String[] args) {
        String cityName = "内蒙古"; // 要获取经纬度的城市名称

        try {
            // 将城市名称进行 URL 编码
            String encodedCityName = URLEncoder.encode(cityName, "UTF-8");

            // 构建请求 URL
            String apiUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + encodedCityName + "&output=json&ak=LL4tVRxHjfU3Whobb9ZQwrU3MSxgGruV";

            // 发送 HTTP GET 请求
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 读取 API 响应数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 解析 JSON 数据
            // 这里使用的是百度地图的 API 响应格式，请根据实际使用的地图服务进行解析
            // 示例中假设 API 响应的数据结构为 {"result":{"location":{"lng":xxx,"lat":xxx}}}
            double cityLng = Double.parseDouble(response.toString().split("\"lng\":")[1].split(",")[0]);
            double cityLat = Double.parseDouble(response.toString().split("\"lat\":")[1].split("}")[0]);
            BigDecimal lng = BigDecimal.valueOf(cityLng);
            BigDecimal lat = BigDecimal.valueOf(cityLat);
            System.out.println("经度：" + lng);
            System.out.println("纬度：" + lat);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
